package com.zy.imagineCommunity.service;

import com.zy.imagineCommunity.dao.PostMapper;
import com.zy.imagineCommunity.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PostService {

    @Autowired
    PostMapper postMapper;

    public void addPost(Post post) {
        postMapper.addPost(post);
    }

    //根据类型、起始条数、每页个数查询文章
    public Page<PostText> queryPostByPageNoAndPageSize(int pageNo, int pageSize) {
        Page<PostText> page = new Page<PostText>();
        //设置当前页码
        page.setPageNo(pageNo);
        //设置每页个数
        page.setPageSize(pageSize);
        //设置总记录数
        int pageCountTotal = postMapper.queryPostCountTotal();
        page.setPageCountTotal(pageCountTotal);
        //设置总页码
        int pageTotal = pageCountTotal / pageSize;
        if (pageCountTotal % pageSize > 0) {
            pageTotal++;
        }
        page.setPageTotal(pageTotal);
        //计算起始条数，查询内容
        int begin = (pageNo - 1) * pageSize;
        List<PostText> postList = postMapper.queryPostByBeginAndPageSize(begin, pageSize);
        //设置每页内容
        page.setItems(postList);
        return page;
    }

    //通过id删除帖子
    public void deletePostById(int id) {
        postMapper.deletePostById(id);
    }

    //帖子详情
    public PostText queryPostById(int id) {
        return postMapper.queryPostById(id);
    }

    //起始条数、每页个数查询文章
    public Page<ChatText> queryChatTextByPostIdAndBeginAndPageSize(int postId, int pageNo, int pageSize) {
        Page<ChatText> page = new Page<ChatText>();
        //设置当前页码
        page.setPageNo(pageNo);
        //设置每页个数
        page.setPageSize(pageSize);
        //设置总记录数
        int pageCountTotal = postMapper.queryChatTextCountTotalByPostId(postId);
        page.setPageCountTotal(pageCountTotal);
        //设置总页码
        int pageTotal = pageCountTotal / pageSize;
        if (pageCountTotal % pageSize > 0) {
            pageTotal++;
        }
        page.setPageTotal(pageTotal);
        //计算起始条数，查询内容
        int begin = (pageNo - 1) * pageSize;
        List<ChatText> chatList = postMapper.queryChatTextByPostIdAndBeginAndPageSize(postId, begin, pageSize);
        //设置每页内容
        page.setItems(chatList);
        return page;
    }

    //增加聊天
    public void addChat(Chat chat) {
        postMapper.addChat(chat);
    }

    /**
     * 主管理员首页查询帖子总数
     *
     * @return
     */
    public int queryPostCountTotal() {
        return postMapper.queryPostCountTotal();
    }


    /**
     * 个人管理帖子
     *
     * @param userId
     * @param pageNo
     * @param pageSize
     * @return
     */
    public Page<PostText> queryPostByUserIdAndPageNoAndPageSize(int userId, int pageNo, int pageSize) {
        Page<PostText> page = new Page<PostText>();
        //设置当前页码
        page.setPageNo(pageNo);
        //设置每页个数
        page.setPageSize(pageSize);
        //设置总记录数
        int pageCountTotal = postMapper.queryPostCountTotalByUserId(userId);
        page.setPageCountTotal(pageCountTotal);
        //设置总页码
        int pageTotal = pageCountTotal / pageSize;
        if (pageCountTotal % pageSize > 0) {
            pageTotal++;
        }
        page.setPageTotal(pageTotal);
        //计算起始条数，查询内容
        int begin = (pageNo - 1) * pageSize;
        List<PostText> postList = postMapper.queryPostByUserIdAndBeginAndPageSize(userId, begin, pageSize);
        //设置每页内容
        page.setItems(postList);
        return page;
    }

    /**
     * 通过postId删除聊天
     *
     * @param postId
     * @return
     */
    public void deleteChatByPostId(int postId) {
        postMapper.deleteChatByPostId(postId);
    }

    /**
     * 通过chatId删除聊天
     *
     * @param chatId
     * @return
     */
    public void deleteChatByChatId(int chatId) {
        postMapper.deleteChatByChatId(chatId);
    }

    /**
     * 搜索帖子
     *
     * @param pageNo
     * @param pageSize
     * @param searchValue
     * @return
     */
    public Page<PostText> queryPostByPageNoAndPageSizeAndSearchValue(int pageNo, int pageSize, String searchValue) {
        Page<PostText> page = new Page<PostText>();
        //设置当前页码
        page.setPageNo(pageNo);
        //设置每页个数
        page.setPageSize(pageSize);
        //设置总记录数
        int pageCountTotal = postMapper.queryPostCountTotalBySearchValue(searchValue);
        page.setPageCountTotal(pageCountTotal);
        //设置总页码
        int pageTotal = pageCountTotal / pageSize;
        if (pageCountTotal % pageSize > 0) {
            pageTotal++;
        }
        page.setPageTotal(pageTotal);
        //计算起始条数，查询内容
        int begin = (pageNo - 1) * pageSize;
        List<PostText> postList = postMapper.queryPostByBeginAndPageSizeAndSearchValue(begin, pageSize, searchValue);
        //设置每页内容
        page.setItems(postList);
        return page;
    }

    /**
     * 搜索个人帖子
     *
     * @param userId
     * @param pageNo
     * @param pageSize
     * @param searchValue
     * @return
     */
    public Page<PostText> searchPostByUserIdAndPageNoAndPageSizeAndSearchValue(int userId, int pageNo, int pageSize, String searchValue) {
        Page<PostText> page = new Page<PostText>();
        //设置当前页码
        page.setPageNo(pageNo);
        //设置每页个数
        page.setPageSize(pageSize);
        //设置总记录数
        int pageCountTotal = postMapper.queryPostCountTotalByUserIdAndSearchValue(userId, searchValue);
        page.setPageCountTotal(pageCountTotal);
        //设置总页码
        int pageTotal = pageCountTotal / pageSize;
        if (pageCountTotal % pageSize > 0) {
            pageTotal++;
        }
        page.setPageTotal(pageTotal);
        //计算起始条数，查询内容
        int begin = (pageNo - 1) * pageSize;
        List<PostText> postList = postMapper.queryPostByUserIdAndBeginAndPageSizeAndSearchValue(userId, begin, pageSize, searchValue);
        //设置每页内容
        page.setItems(postList);
        return page;
    }

}
